/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ta.entity.Updatetime;
import com.thinkgem.jeesite.modules.ta.service.StWorkflowService;
import com.thinkgem.jeesite.modules.ta.service.UpdatetimeService;

/**
 * 更新时间Controller
 * @author 刘雨轩
 * @version 2017-05-31
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/updatetime")
public class UpdatetimeController extends BaseController {

	@Autowired
	private StWorkflowService stWorkflowService;
	
	@Autowired
	private UpdatetimeService updatetimeService;
	
	@RequiresPermissions("ta:updatetime:view")
	@RequestMapping(value = "formiid")
	public String form(String iid, Model model) {
		List<Updatetime> ls = updatetimeService.find(iid);
		model.addAttribute("ls", ls);
		return "modules/ta/updatetimeForm";
	}
	
	@RequiresPermissions("ta:updatetime:view")
	@RequestMapping(value = {"list", ""})
	public String list(Updatetime updatetime, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Updatetime> page = updatetimeService.findPage(new Page<Updatetime>(request, response), updatetime); 
		List<String> ls = stWorkflowService.findName();
		model.addAttribute("ls", ls);
		model.addAttribute("page", page);
		return "modules/ta/updatetimeList";
	}

	@RequiresPermissions("ta:updatetime:view")
	@RequestMapping(value = "form")
	public String form(Updatetime updatetime, Model model) {
		model.addAttribute("updatetime", updatetime);
		return "modules/ta/updatetimeForm";
	}

	@RequiresPermissions("ta:updatetime:edit")
	@RequestMapping(value = "save")
	public String save(Updatetime updatetime, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, updatetime)){
			return form(updatetime, model);
		}
		updatetimeService.save(updatetime);
		addMessage(redirectAttributes, "保存更新时间成功");
		return "redirect:"+Global.getAdminPath()+"/ta/updatetime/?repage";
	}
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@RequiresPermissions("ta:updatetime:edit")
	@RequestMapping(value = "updateTime")
	public String updateTime(String [] str, Model model, RedirectAttributes redirectAttributes) throws ParseException {
		for (String string : str) {
			int index = string.indexOf("_");
			String iid = string.substring(0, index);
			String strtime = string.substring(index+1);
			Date time = sdf.parse(strtime);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("iid", iid);
			map.put("time", time);
			updatetimeService.updateTime(map);
		}
		addMessage(redirectAttributes, "保存更新时间成功");
		return "redirect:"+Global.getAdminPath()+"/ta/updatetime/?repage";
	}
	@RequiresPermissions("ta:updatetime:edit")
	@RequestMapping(value = "updateTimes")
	public String updateTimes(String [] str, Model model, RedirectAttributes redirectAttributes) throws ParseException {
		for (String string : str) {
			String atime = string.substring(string.indexOf("@")+1, string.lastIndexOf("@"));
			String stime = string.substring(string.lastIndexOf("@")+1);
			String wiid = string.substring(0, string.indexOf("@"));
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("atime", sdf.parse(atime));
			map.put("wiid", wiid);
			if(stime != null && !"".equals(stime)){
				map.put("stime", sdf.parse(stime));
			}else{
				map.put("stime", null);
			}
			updatetimeService.updateTimes(map);
		}
		addMessage(redirectAttributes, "保存更新时间成功");
		return "redirect:"+Global.getAdminPath()+"/ta/updatetime/?repage";
	}
	
	@RequiresPermissions("ta:updatetime:edit")
	@RequestMapping(value = "delete")
	public String delete(Updatetime updatetime, RedirectAttributes redirectAttributes) {
		updatetimeService.delete(updatetime);
		addMessage(redirectAttributes, "删除更新时间成功");
		return "redirect:"+Global.getAdminPath()+"/ta/updatetime/?repage";
	}

}